# encoding=utf-8
import unittest,random,sys
from time import sleep
import time
from selenium.webdriver.common.by import By
from DriverAndUnit import myunit,myfunction
from LbtFront.Base.LoginPage import login
from Resources import UserName
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class sendmessage(myunit.myTest,login):
    '''发短信测试'''
    def chioceTeacher(self):
        '''给老师发短信'''
        login(self.driver).open()
        login(self.driver).user_login(UserName.lbtUserName, UserName.lbtUserPassword)
        sleep(3)
        po = login(self.driver)
        '''选择学校，并登录，且验证登录后的用户名是否正确，并拍照'''
        po.choiceSchool('btn_rolse', '乐贝通幼儿园')
        # 打开短信发送模块
        self.driver.find_element_by_class_name('icon-sms').click()
        smslist = self.driver.find_elements_by_link_text('短信发送')
        self.assertEqual(len(smslist), 1)
        smslist[0].click()
        sleep(1)
        # 选择老师
        self.driver.switch_to_frame('iframe')
        # 切换到老师
        self.driver.find_element_by_link_text('发给老师').click()
        self.driver.find_element_by_id('selectUserArea').click()
        # self.driver.switch_to.parent_frame()  这一行是可以切换到父frame
        '''注意，一般通过text找不到的对象，可以通过id、classname、name找到，如果都找不到就试一下xpath，仍然不行需要考虑不再一个frame下面'''
        self.driver.switch_to_frame('layui-layer-iframe2')
        listItem = self.driver.find_elements_by_class_name('panel-heading')
        listItem[0].click()
        # self.driver.find_element_by_partial_link_text('22').click()
        teachers = self.driver.find_elements_by_name('switchStuId')
        for teacher in teachers:
            if teacher.get_attribute('disabled') != 'true':
                teacher.click()
                studentname = teacher.get_attribute('data-tname')
                #print(teacher.get_attribute('data-tname'))
                break
        self.driver.switch_to.parent_frame()
        self.driver.find_element_by_class_name('layui-layer-btn0').click()
        self.assertIn(studentname, self.driver.find_element_by_id('selectUserArea').text)
        #print('选择学生完毕')
        return studentname
    def chiocestudent(self):
        '''给学生发短信'''
        login(self.driver).open()
        login(self.driver).user_login(UserName.lbtUserName, UserName.lbtUserPassword)
        sleep(3)
        po = login(self.driver)
        '''选择学校，并登录，且验证登录后的用户名是否正确，并拍照'''
        po.choiceSchool('btn_rolse', '乐贝通幼儿园')
        # 打开短信发送模块
        self.driver.find_element_by_class_name('icon-sms').click()
        smslist = self.driver.find_elements_by_link_text('短信发送')
        self.assertEqual(len(smslist), 1)
        smslist[0].click()
        sleep(1)
        # 选择学生
        self.driver.switch_to_frame('iframe0')
        self.driver.find_element_by_id('selectUserArea').click()
        #print(self.driver.find_element_by_xpath("//div[@id='layui-layer1']").get_attribute("style"))
        '''修改页面元素属性---！！！'''
        js2 = "document.getElementById('layui-layer1').setAttribute('style','z-index: 19891015; width: 697px; height: 600px; top: -50px; left: 230px;')"
        self.driver.execute_script(js2)
        '''
        直接操作页面元素
        ele = driver.find_element_by_id('reg_butt')
js1 = "arguments[0].click()"
driver.execute_script(js1,ele)
        '''
        #print(self.driver.find_element_by_xpath("//div[@id='layui-layer1']").get_attribute("style"))
        # self.driver.switch_to.parent_frame()  这一行是可以切换到父frame
        '''注意，一般通过text找不到的对象，可以通过id、classname、name找到，如果都找不到就试一下xpath，仍然不行需要考虑不再一个frame下面'''
        self.driver.switch_to.frame('layui-layer-iframe1')

        listItem = self.driver.find_elements_by_class_name('class-name')
        #遍历每一个班级
        for itemBan in listItem:
            itemBan.click()
        #把学生信息都拿到
        listStudents=self.driver.find_elements_by_name("switchStuId")
        studentname=""
        for studentInfo in listStudents:
            #如果学生订购了就拿到phone，然后两次分割，再去掉0，
            # 并找到手机号再测试范围内的就选中发短信。
            #这里去掉0其实可以省略，如果严谨一点应该去掉1、A、J、K等。
            if studentInfo.get_attribute("disabled")==None:
                dataphone=str(studentInfo.get_attribute("data-phone"))
                phoneInfos=dataphone.split(",")
                phoneInfo=phoneInfos[0]
                #for phoneInfo in phoneInfos:
                phoneAndType=phoneInfo.split("_")
                for phoneTrue in phoneAndType:
                    if ((len(phoneTrue))==11 and (phoneTrue in UserName.receiverPhoneForTest)):
                        studentInfo.click()
                        studentname=studentInfo.get_attribute("data-tname")
                        break
            if studentname!="":
                break
        self.driver.switch_to.parent_frame()
        #self.driver.switch_to.default_content()
        self.driver.find_element_by_class_name('layui-layer-btn0').click()
        #self.driver.find_element_by_xpath("//a[@class-name='layui-layer-btn0']").click()
        testT=self.driver.find_element_by_id('selectUserArea').text
        self.assertIn(studentname, testT)
        #print('选择学生完毕')
        return studentname
    def writeContent(self,content):
        '''填写短信发送内容'''
        smsInfo = content + time.strftime('%Y%m%d%H%M%S')
        self.driver.find_element_by_id('content').send_keys(smsInfo)
        return smsInfo
    def getTimintInfo(self):
        '''计算定时发送时间'''
        return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()+60*60))
    def checkSmsIsSuccess(self,smsInfo,studentname):
        '''查询之前短信是否真实发送成功'''
        isSuccess=False
        self.driver.switch_to.parent_frame()
        self.driver.find_element_by_xpath("//a[@href='http://www.lebeitong.com:80/sms/toMyReceive.jhtml']").click()
        self.driver.switch_to_frame('iframe1')
        self.driver.find_element_by_link_text('我发送的').click()
        self.driver.find_element_by_id('txtKeyword').send_keys(smsInfo)
        self.driver.find_elements_by_class_name('btn-primary')[0].click()
        smsReceiveInfoList = self.driver.find_elements_by_class_name('smsReceive')
        if len(smsReceiveInfoList) > 0:
            smsReceiveInfoList[0].click()
            self.driver.switch_to_frame('layui-layer-iframe1')
            receiverstudentnames = self.driver.find_elements_by_xpath(
                '//div[@id="smsReceive"]/section/table/tbody/tr[1]/td')
            if receiverstudentnames[1].text==studentname:
                isSuccess=True
        return isSuccess
    def test_studentSendNow(self):
        '''给学生发及时短信'''
        try:
            # 选择学生
            studentname = self.chiocestudent()
            # 填写短信内容
            smsInfo = self.writeContent('向' + studentname + '家长发及时短信，时间为：')
            # 发短信按钮
            self.driver.find_element_by_id('btnSubmit').click()
            #print('学生及时短信发送完毕')
            self.assertEqual(self.checkSmsIsSuccess(smsInfo, studentname), True)
            #print('校验短信接收情况完毕')
        finally:
            errorPng = "FinallyPng_" + myfunction.getCurrentTime() + ".png"
            myfunction.insert_img(self.driver, errorPng)
    def test_studentSendTiming(self):
        '''给学生发定时短信'''
        try:
            # 选择学生
            studentname = self.chiocestudent()
            # 填写短信内容
            smsInfo = self.writeContent('向' + studentname + '家长发定时短信，当前发送时间为：')
            self.driver.find_elements_by_name('timing')[1].click()
            self.driver.find_element_by_id('timingTime').send_keys(self.getTimintInfo())
            self.driver.find_element_by_class_name('btn-success').click()
            # js2 = "document.getElementByClass('col-sm-offset-2').setAttribute('style','margin-top:-20px')"
            '''通过css定位元素并修改元素属性'''
            js2 = "document.querySelector('.col-sm-offset-2').style='margin-top:-100px'"
            self.driver.execute_script(js2)
            self.driver.find_element_by_id('btnSubmit').click()
            #print('学生定时短信发送完毕')
            self.assertEqual(self.checkSmsIsSuccess(smsInfo, studentname), True)
            #print('校验定时短信发送情况完毕')
        finally:
            errorPng = "FinallyPng_" + myfunction.getCurrentTime() + ".png"
            myfunction.insert_img(self.driver, errorPng)
    """
    def test_teacherSendNow(self):
        '''给老师发及时短信'''

       
    def test_studentSendNow(self):
        '''给老师发定时短信'''
    def test_studentSendNow(self):
        '''短信模板校验'''
        """
if __name__=='__main__':
    unittest.main()